package com.kgis.fm.common.manager
{
	import com.kgis.fm.flex.model.constant.LayerTypeConstant;
	import com.kgis.fm.flex.model.manager.FmMetaData;
	import com.kgis.fm.flex.model.term.LegendEntry;
	import com.kgis.fm.flex.model.term.LegendEntryDefinition;
	import com.kgis.fm.flex.model.term.LegendEntryDisplayControl;
	import com.kgis.map.Map;
	import com.kgis.map.layer.FeatureLayer;
	import com.kgis.map.rule.Rule;
	import com.kgis.map.style.IStyle;
	import com.kgis.map.style.LabelStyle;
	import com.kgis.map.style.PointStyle;
	import com.kgis.map.style.PolygonStyle;
	import com.kgis.map.style.PolylineStyle;
	import com.kgis.map.style.StyleRule;
	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;

	public class BusinessLayerManager
	{
		public function BusinessLayerManager()
		{
		}
		/**
		 * 初始化数据库的矢量图层
		 */
		public static function initFeatureLayerByMetaData(legendId:int,kgisMap:Map):void
		{
			var legendEntryDefinition:LegendEntryDefinition=null;
			var labelLegendEntryDefinition:LegendEntryDefinition=null;
			var legendEntry:LegendEntry=null;
			var labelLegendEntry:LegendEntry=null;
			
			var metaData:FmMetaData=MetaDataManager.getInstance().getMetaData(MetaDataManager.KEY_KGM_METAData) as FmMetaData;
			//遍legendEntry历所有的legenddspctrl
			for each (var legendDspCtrl:LegendEntryDisplayControl in metaData.legendEntryDisplayMap[legendId])
			{
				if (legendDspCtrl.leno > 0)
				{ 
					legendEntry=MetaDataManager.getLegendEntryById(legendDspCtrl.leno);
					if (legendEntry)
					{
						//如果图层是动态标注，不处理，在它主图层上处理
						if(legendEntry.layerType==LayerTypeConstant.LAYERTYPE_TEXT&&legendEntry.bautoLabel) continue;
						
						legendEntryDefinition=MetaDataManager.getLegendEntryDefByEntryId(legendId, legendEntry.leno);
						
						//插入新的图层
						var feaLayer:FeatureLayer=new FeatureLayer(legendEntry.leno, legendEntry.name);
						feaLayer.geometryType=legendEntry.layerType;
						
		
						//样式规则
						feaLayer.geoStyleRule=StyleRuleManager.getStyleRule(legendEntry,kgisMap);
						feaLayer.minZoomLevel=legendEntryDefinition.displayLevelMin;
						feaLayer.maxZoomLevel=legendEntryDefinition.displayLevelMax;
						feaLayer.minZoomLevelLabel=legendEntryDefinition.displayLevelMin;
						feaLayer.maxZoomLevelLabel=legendEntryDefinition.displayLevelMax;
						feaLayer.levelable=legendEntryDefinition.scalable;
						feaLayer.visible=legendEntryDefinition.displayMode;
						feaLayer.selectable=legendEntryDefinition.selectable;
						var rule:Rule=StyleRuleManager.getStyleRule(legendEntry,kgisMap);
						var sr:StyleRule=rule.expressionFilterDic.getItemAt(rule.expressionFilterDic.length-1) as StyleRule;
						feaLayer.geoStyle=sr.style;
						
												
						//获取自动标注图层
						labelLegendEntry=MetaDataManager.getLegendEntryById(legendEntry.autoLabelLeno);
						if (labelLegendEntry)
						{
							labelLegendEntryDefinition=MetaDataManager.getLegendEntryDefByEntryId(legendId, labelLegendEntry.leno);
							feaLayer.hasAutoLabel=true;
						}
						
						//静态标注
						if(legendEntry.layerType==LayerTypeConstant.LAYERTYPE_TEXT)
						{
							labelLegendEntry=legendEntry;
							labelLegendEntryDefinition=legendEntryDefinition;
							
						}
						
						//标注规则
						if (labelLegendEntryDefinition)
						{
							feaLayer.minZoomLevelLabel=labelLegendEntryDefinition.displayLevelMin;
							feaLayer.maxZoomLevelLabel=labelLegendEntryDefinition.displayLevelMax;
							feaLayer.labelVisible=labelLegendEntryDefinition.displayMode;
							feaLayer.labelLevelable=labelLegendEntryDefinition.scalable;
						}
						if(labelLegendEntry)
						{
							feaLayer.labelStyleRule=StyleRuleManager.getStyleRule(labelLegendEntry,kgisMap);
							feaLayer.labelContentRule=LabelRuleManager.getLabelRule(labelLegendEntry);
							var rule:Rule=StyleRuleManager.getStyleRule(labelLegendEntry,kgisMap);
							var sr:StyleRule=rule.expressionFilterDic.getItemAt(rule.expressionFilterDic.length-1) as StyleRule;
							feaLayer.labelStyle=sr.style;
						}
						
						
						
						
						kgisMap.addLayer(feaLayer);
						
					}
				}
			}
			
			//排序每个图层
			var layerindex:int=0;
			for each (var legendEntryDef:LegendEntryDefinition in metaData.legendEntryDefList)
			{
				if (legendEntryDef.leno == legendId)
				{
					var fl:FeatureLayer=kgisMap.getLayerById(legendEntryDef.leno) as FeatureLayer;
					if (fl)
					{
						kgisMap.changeLayerIndex(fl, layerindex);
						layerindex++;
					}
				}
			}
			
		}
	}
}